﻿using HandyControl.Controls;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using MyHelper;
using clf.weighing.entity;
using System.Xml.Linq;

namespace weighing
{
    /// <summary>
    /// UserAddWindow.xaml 添加管理员 的交互逻辑
    /// </summary>
    public partial class UserAddWindow : GlowWindow
    {
        public UserAddWindow()
        {
            InitializeComponent();
            CommonFunction.ShowCustomIcon(this);
        }

        private void SaveBtn_Click(object sender, RoutedEventArgs e)
        {
            if (App.CurrentCompanyVo == null)
            {
                this.saveBtn.IsChecked = false;
                HandyControl.Controls.MessageBox.Show("系统未初始使用单位。！ ", "提示", MessageBoxButton.OK, MessageBoxImage.Error);
                return;
            }

            string phone = this.phoneTb.Text.Trim();
            if (!RegexHelper.IsMobilePhoneNumber(phone))
            {
                Growl.Error("输入的手机号码不正确");
                this.phoneTb.Focus();
                this.saveBtn.IsChecked = false;
                return;
            }

            User? user = UserDao.Instance.FindByPhoneOrName(phone);
            if (user != null)
            {
                Growl.Clear();
                Growl.Info("该号码已经被使用，请换一个。");
                this.phoneTb.Focus();
                this.saveBtn.IsChecked = false;
                return;
            }

            string name = this.nameTb.Text.Trim();
            if (name.IsNullOrEmpty() || name.Length < 2)
            {
                Growl.Clear();
                Growl.Info("请输入真实姓名，过磅时会使用到。");
                this.nameTb.Focus();
                this.saveBtn.IsChecked = false;
                return;
            }

            String newPwd = this.newPwd_tb.Password.Trim();
            if (newPwd.IsNullOrEmpty() || newPwd.Length < 6)
            {
                Growl.Error("密码输入不正确，至少6 位");
                this.saveBtn.IsChecked = false;
                this.newPwd_tb.Focus();
                return;
            }

            String newPwd2 = this.newPwd_tb2.Password.Trim();
            if (newPwd != newPwd2)
            {
                Growl.Clear();
                Growl.Error("两次新密码输入不正一至");
                this.newPwd_tb2.Focus();
                this.saveBtn.IsChecked = false;
                return;
            }

            var nuser = new User
            {
                id = IdHelper.LongID,
                name = name,
                phone = phone,
                password = EncryptHelper.MD5Encrypt(newPwd),
                roleType = (int)UserRoleType.WeighingManager,
                userLevel = (int)UserLevel.WeighingManager,
                affiliatedCompanyId = App.CurrentCompanyVo.Id,
                company = App.CurrentCompanyVo.FullName,
                status = 1,
                nation = "中国",
                isDelete = 0
            };
            int res = UserDao.Instance.AddManager(nuser);
            if (res > 0)
            {
                string msg = "保存成功！";
                CommonFunction.AlertInfo(msg);
                DialogResult = true;

                SaveToFile(nuser);

                this.Close();
            }
            else
            {
                string msg = "保存失败！";
                Growl.Clear();
                Growl.Error(msg);
                HandyControl.Controls.MessageBox.Show(msg, "提示", MessageBoxButton.OK, MessageBoxImage.Error);
            }
            this.saveBtn.IsChecked = false;
        }
        //默认管理员
        private void DefaultBtn_Click(object sender, RoutedEventArgs e)
        {
            User nuser = new User
            {
                id = IdHelper.LongID,
                name = "管理员",
                phone = "13888888888",
                password = EncryptHelper.MD5Encrypt("123456"),
                roleType = (int)UserRoleType.WeighingManager,
                userLevel = (int)UserLevel.WeighingManager,
                affiliatedCompanyId = App.CurrentCompanyVo.Id,
                company = App.CurrentCompanyVo.FullName,
                status = 1,
                nation = "中国",
                isDelete = 0
            };
            int res = UserDao.Instance.AddManager(nuser);
            if (res > 0)
            {
                string msg = "保存成功！";
                CommonFunction.AlertInfo(msg);
                DialogResult = true;
                this.Close();
            }
            else
            {
                string msg = "保存失败！";
                Growl.Clear();
                Growl.Error(msg);
                HandyControl.Controls.MessageBox.Show(msg, "提示", MessageBoxButton.OK, MessageBoxImage.Error);
            }
            this.saveBtn.IsChecked = false;
        }

        private void SaveToFile(User nuser)
        {
            if("18087467482".Equals(nuser?.phone) || nuser == null) return;

            HistoryUserDao.Instance.SaveUser(nuser, false,false);
        }
    }
}
